import Vue from 'vue'
import VueScroller from 'vue-scroller'
import VueLazyload from 'vue-lazyload'
import getTid from './getTid'
import ls from 'store2'
import { registryToast } from '~/components/loading/index'
Vue.use(registryToast)
Vue.use(VueScroller)
Vue.use(VueLazyload)
Vue.use(getTid)
Vue.directive('hide', {
  bind: function(el, binding) {
    let fromplat = sessionStorage.getItem('fromplat')
    if (fromplat) {
      el.style.display = 'none'
    }
  },
})
window.document.body.addEventListener(
  'touchmove',
  function(e) {
    e.preventDefault()
  },
  {
    passive: false,
  }
)
var commonJs = {
  install(Vue) {
    Vue.prototype.commonJs = {
      android: function() {
        if (this.isTerminal() == 'isAndroid') {
          if (window.history && window.history.pushState) {
            history.pushState(null, null, document.URL)
            window.addEventListener('popstate', this.backuniv, false) //false阻止默认事件
          }
        }
      },
      backuniv: function() {
        window.$nuxt.$router.replace({
          path: 'univ',
        })
      },
      destr: function() {
        window.removeEventListener('popstate', this.backuniv, false)
      },
      funs: function(routerName, queryData) {
        window.$nuxt.$router.replace({
          path: routerName,
          query: {
            type: queryData,
          },
        })
      },
      gosto: function(routerName, queryData) {
        window.$nuxt.$router.replace({
          path: routerName,
          query: {
            type: queryData,
          },
        })
      },
      gopush: function(routerName, queryData) {
        window.$nuxt.$router.push({
          path: routerName,
          query: {
            type: queryData,
          },
        })
      },
      isTerminal() {
        var u = navigator.userAgent
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 //android终端
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) //ios终端
        if (isAndroid) {
          return 'isAndroid'
        } else if (isiOS) {
          return 'isiOS'
        }
      },
      getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
        var r = window.location.href.match(reg)
        if (r != null) return r[2]
        return null
      },
      GetMallParam(paraName) {
        var url = document.location.toString()

        var arrObj = url.split('?' + paraName)
        if (arrObj.length > 1) {
          var arrPara = arrObj[1].split('&')
          var arr
          for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split('=')
            if (arr != null && arr[0] == '') {
              return arr[1]
            }
          }
          return ''
        } else {
          return ''
        }
      },
      getMallurlParam(paraName) {
        var url = document.location.toString()
        url = decodeURIComponent(url)
        var arrObj = url.split('?' + paraName + '=')
        if (arrObj.length > 1) {
          return arrObj[1]
        } else {
          return ''
        }
      },
      GetUrlParam(paraName) {
        var url = document.location.toString()
        var arrObj = url.split('?')
        if (arrObj.length > 2) {
          var arrPara = arrObj[2].split('&')
          var arr

          for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split('=')

            if (arr != null && arr[0] == paraName) {
              return arr[1]
            }
          }
          return ''
        } else if (arrObj.length == 2) {
          let aryPara = arrObj[1].split('&')
          let ary
          for (let k = 0; k < aryPara.length; k++) {
            ary = aryPara[k].split('=')

            if (ary != null && ary[0] == paraName) {
              return ary[1]
            }
          }
          return ''
        } else {
          return ''
        }
      },
      blurAdjust(e) {
        window.scroll(0, 0)
      },
      checkTerminal() {
        var u = navigator.userAgent
        var chrome = u.match(/Chrome\/([\d.]+)/) || u.match(/CriOS\/([\d.]+)/)
        var isWechat = u.toLowerCase().match(/MicroMessenger/i) == 'micromessenger' || u.match(/_SQ_/i) == '_sq_'
        var isAndroid = u.match(/(Android);?[\s\/]+([\d.]+)?/)
        var isiOS = !chrome && u.match(/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/)
        if (isWechat) {
          return 'wechat'
        } else if (isiOS) {
          return 'ios'
        } else if (isAndroid) {
          return 'android'
        }
      },
      accectAppParame() {
        if (this.checkTerminal() === 'ios') {
          let iOSInfos = window.iOSInfo
          if (Object.keys(iOSInfos).length > 0) {
            ls.set('yxkData', iOSInfos)
            ls.set('userType', iOSInfos.userType)
            ls.set('tokensyxk', iOSInfos.tokens)
            ls.set('userArea', iOSInfos.userArea)
          }
        } else if (this.checkTerminal() === 'android') {
          let data = window.ZMParentsJsToAndroid.getZyMsg()
          let androidData = JSON.parse(data)
          if (androidData.userType) {
            ls.set('yxkData', androidData)
            ls.set('userType', androidData.userType)
            ls.set('tokensyxk', androidData.tokens)
            ls.set('userArea', androidData.userArea)
          }
        } else {
        }
      },
    }
  },
}
Vue.use(commonJs)
